package com.example.myapplication;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.example.myapplication.adapter.MyAdapter;
import com.example.myapplication.pojo.Music;

import java.util.ArrayList;

public class MusicActivty extends AppCompatActivity {

    private static final String TAG = "MusicActivty";
    private Button btn;
    private ListView listView;
    private ContentResolver resolver;
    private ArrayList<Music> musicList;
    private boolean grantMusic;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_activty);

        initView();
        initEvent();
    }

    private void initEvent() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                readMusic();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MusicActivty.this, MainActivity.class);

                intent.putExtra("musicPath", musicList.get(position).getMusicPath());

                startActivity(intent);
            }
        });

    }

    private void initView() {
        btn = findViewById(R.id.btn);
        listView = findViewById(R.id.misic_list);

    }

    private void handlePermission() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            int permission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
            if(permission == PackageManager.PERMISSION_GRANTED){
                grantMusic = true;
                return;
            }else{
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},101);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 101 )
        {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                grantMusic = true;
                Toast.makeText(this, "读取授权成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void readMusic() {

        resolver = getContentResolver();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
            int permission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
            if(permission != 0){
                Toast.makeText(this, "没有授权", Toast.LENGTH_SHORT).show();
                handlePermission();
                return;
            }
        }


        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
                , new String[]{MediaStore.Audio.Media._ID
                        , MediaStore.Audio.Media.TITLE
                        , MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.DATA }
                , null, null, null);

        musicList = new ArrayList();
        while (cursor.moveToNext()) {
            Music music = new Music();
            music.setId(cursor.getString(0));
            music.setMusicName(cursor.getString(1));
            music.setSinger(cursor.getString(2));
            music.setMusicPath(cursor.getString(3));
            Log.e(TAG, "readMusic: "+cursor.getString(3) );

            musicList.add(music);
        }

        listView.setAdapter(new MyAdapter(MusicActivty.this, musicList));
    }


}
